﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;




namespace eRechnung_3_0
{
    public class InvoiceXSD
    {
        private XmlDocument xsdDoc;
        private XmlNamespaceManager xMan;

        public InvoiceXSD()
        {
            Assembly Asm = Assembly.GetExecutingAssembly();
            Stream St = Asm.GetManifestResourceStream("eRechnung_3_0.Invoice.xsd");
            StreamReader ResRd = new StreamReader(St);
            xsdDoc = new XmlDocument();
            if (ResRd != null)
            {
                xsdDoc.Load(ResRd);
            }
            xMan = new XmlNamespaceManager(xsdDoc.NameTable);

            xMan.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        }

        private String[] GetAllValues(string xPath)
        {
            XmlNodeList xNodeList = xsdDoc.SelectNodes(@xPath, xMan);

            string[] enumVal = new string[xNodeList.Count];
            int ctr = 0;

            foreach (XmlNode xNode in xNodeList)
            {
                enumVal[ctr] = xNode.Attributes["value"].Value;
                ctr++;
            }
            return (enumVal);

        }

        public String[] GetAllCurrencies()
        {
            string Path ="xs:schema/xs:simpleType[@name = 'CurrencyType']/xs:restriction/xs:enumeration";
            string[] enumVal = GetAllValues(Path);
            return (enumVal);
        }

        public void FillCountryCCbox(ComboBox lb)
        {
            String[] Countries = this.GetAllCurrencies();
            lb.Items.Clear();
            foreach (string Curr in Countries)
            {
                lb.Items.Add(Curr);
            }

            if (lb.Text.ToString() == "")
            {
                lb.Text = "EUR";
            }

        }

        public String[] GetAllInvoiceTypes()
        {
            string Path = "xs:schema/xs:simpleType[@name = 'DocumentTypeType']/xs:restriction/xs:enumeration";
            string[] enumVal = GetAllValues(Path);
            return (enumVal);

        }

    }
}
